package com.inspur.gs.sbssdl.sbyxgl.wxjl.wxjl.back.bizactions;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.inspur.edp.bef.api.be.IBEManagerContext;
import com.inspur.edp.bef.api.be.IBEService;
import com.inspur.edp.bef.api.be.IBusinessEntity;
import com.inspur.edp.bef.api.lcp.ILcpFactory;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.api.services.IBefSessionManager;
import com.inspur.edp.bef.engine.entity.AssociationInfo;
import com.inspur.edp.bef.spi.action.AbstractManagerAction;
import com.inspur.edp.cdp.coderule.api.CodeRuleBEAssignService;
import com.inspur.edp.cef.entity.changeset.ModifyChangeDetail;
import com.inspur.edp.cef.entity.entity.IEntityData;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class sbssWxjlEditWxjlMgrAction extends AbstractManagerAction<String> {
    private String wxjl;

    public sbssWxjlEditWxjlMgrAction(IBEManagerContext managerContext, String wxjl) {
        super(managerContext);
        this.wxjl = wxjl;
    }

    private static final CodeRuleBEAssignService rule = SpringBeanUtils.getBean(CodeRuleBEAssignService.class);

    @Override
    public void execute() {
        SpringBeanUtils.getBean(IBefSessionManager.class).createSession();
        String configID = "3d097fda-c626-461b-81a9-e6040eba0270";
        String beId = "3d097fda-c626-461b-81a9-e6040eba0270";
        String ruleId = "d0083c82-102d-4be6-9a6b-42768faa5a6b";
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> map = null;
        try {

            ILcpFactory iLcpFactory = SpringBeanUtils.getBean(ILcpFactory.class);
            IStandardLcp lcp = iLcpFactory.createLcpByBEId(configID);
            map = mapper.readValue(wxjl, Map.class);
            IEntityData data = lcp.retrieve(map.get("id").toString()).getData();
            ModifyChangeDetail change = new ModifyChangeDetail(data.getID());

            //故障单相关
            AssociationInfo gzdhInfo = new AssociationInfo();
            Map<String, Object> gzdh = (Map<String, Object>) map.get("gzdh");
            gzdhInfo.setValue("gzdh", gzdh.get("gzdh"));
            change.getPropertyChanges().put("gzdh", gzdhInfo);

            //维修人员
            AssociationInfo wxryInfo = new AssociationInfo();
            Map<String, Object> wxry = (Map<String, Object>) map.get("wxry");
            wxryInfo.setValue("wxry_ID", wxry.get("wxry"));
            wxryInfo.setValue("wxry", wxry.get("wxry"));
            change.getPropertyChanges().put("wxry", wxryInfo);

            //维修日期
            String wxrq = map.get("wxrq").toString();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf.parse(wxrq);
            change.getPropertyChanges().put("wxrq", date);

            //处理过程
            if (map.get("clgc") != null) {
                change.getPropertyChanges().put("clgc", map.get("clgc"));
            }

            //耗材
            if (map.get("hc") != null) {
                change.getPropertyChanges().put("hc", map.get("hc"));
            }

            //维修结果
            if (map.get("wxjg") != null) {
                change.getPropertyChanges().put("wxjg", map.get("wxjg"));
            }

            change.getPropertyChanges().put("isTt", map.get("isTt"));

            //停机台时
            BigDecimal tjts = new BigDecimal(map.get("tjts").toString());
            change.getPropertyChanges().put("Tjts", tjts);

            if (map.get("wxxz") != null) {
                change.getPropertyChanges().put("wxxz", map.get("wxxz"));
            }

            if (map.get("bz") != null) {
                change.getPropertyChanges().put("bz", map.get("bz"));
            }

            //存储设备实体字段
            change.getPropertyChanges().put("sbbh", map.get("sbbh"));
            change.getPropertyChanges().put("sbmc", map.get("sbmc"));
            change.getPropertyChanges().put("ggxh", map.get("ggxh"));
            change.getPropertyChanges().put("xh", map.get("xh"));
            change.getPropertyChanges().put("shopName", map.get("shopName"));
            change.getPropertyChanges().put("sbbh_old", map.get("sbbh_old"));
            change.getPropertyChanges().put("lbmc", map.get("lbmc"));
            change.getPropertyChanges().put("ztmc", map.get("ztmc"));
            change.getPropertyChanges().put("abcmc", map.get("abcmc"));
            change.getPropertyChanges().put("bmmc", map.get("bmmc"));

            lcp.modify(change);
            lcp.save();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            SpringBeanUtils.getBean(IBefSessionManager.class).closeCurrentSession();
        }

    }

    private IBusinessEntity getEntity(String dataId) {
        return (IBusinessEntity) super.getBEManagerContext().getEntity(dataId);
    }

    private IBEService getMgr() {
        return (IBEService) super.getBEManagerContext().getBEManager();
    }
}